<template>
  <div class="base-container">
    <t-form ref="form" :data="queryParams" label-width="80"
            @submit="()=>{this.pagination.current=1;this.queryParams.pageNum=1;this.getList()}"
            @reset="()=>{this.pagination.current=1;this.queryParams.pageNum=1;this.getList()}">
      <t-row style="border-bottom: 1px solid #E5E6EB;margin-bottom: 10px">
        <t-col :span="11" class="query-form-inline">
          <t-form-item label="指标类别" name="name">
            <t-input
              v-model="queryParams.name"
              clearable
              placeholder="请输入指标类别"
              style="width: 240px"
            />
          </t-form-item>
        </t-col>
        <t-col :span="1" class="operation-container">
          <t-button theme="primary" type="submit">查询</t-button>
          <t-button theme="default" variant="outline" type="reset">重置</t-button>
        </t-col>
      </t-row>
    </t-form>
    <div class="btn-group-inline">
      <t-button @click="dialog.header='新增指标类别';dialog.objId=0;dialog.parentId=0;dialog.open_add_edit = true;"
                variant="outline" theme="primary">
        <t-icon name="add" slot="icon"/>
        新增
      </t-button>
      <t-button @click="onExpandAllToggle" variant="outline" theme="primary">
        <t-icon name="swap" slot="icon"/>
        展开/折叠
      </t-button>
    </div>
    <t-enhanced-table
      ref="table"
      rowKey="id"
      :data="data"
      :columns="columns"
      hover
      :loading="dataLoading"
      :expandAll="expandAll"
      :expandedTreeNodes.sync="expandedTreeNodes"
      :tree="{ childrenKey: 'children', expandTreeNodeOnClick: true}"
    >
      <template #quantization="{ row }">
        {{ row.quantization === '1' ? '是' : '否' }}
      </template>
      <template #op="{ row }">
        <t-button
          @click.stop="dialog.header='修改指标类别';dialog.row=row;dialog.objId=row.id;dialog.parentId=0;dialog.open_add_edit = true;"
          size="small" variant="text" class="t-button-link">
          <t-icon name="edit-1" slot="icon"/>
          修改
        </t-button>
        <t-button size="small"
                  @click.stop="dialog.header='新增指标类别';dialog.objId=0;dialog.parentId=row.id;dialog.open_add_edit = true;"
                  variant="text" class="t-button-link">
          <t-icon name="add" slot="icon"/>
          新增
        </t-button>
        <t-button theme="danger" @click.stop="handleDelete(row)" size="small" variant="text" class="t-button-link">
          <t-icon name="delete" slot="icon"/>
          删除
        </t-button>
      </template>
    </t-enhanced-table>
    <add-edit-kpi @reload="getList" :header="dialog.header" :parentId="dialog.parentId" :objId="dialog.objId"
                  :visible.sync="dialog.open_add_edit" v-if="dialog.open_add_edit"></add-edit-kpi>
  </div>
</template>

<script>
import addEditKpi from "./components/add-edit-kpi.vue";

export default {
  name: "kpi-index",
  components: {
    addEditKpi
  },
  data() {
    return {
      expandAll: false,
      expandedTreeNodes: [],
      queryParams: {
        name: '',
      },
      data: [],
      columns: [
        {
          colKey: "name",
          title: "指标类别",
        },
        {
          colKey: "quantization",
          title: "是否可量化",
          align: "center",
        },
        {
          colKey: "sort",
          title: "排序",
          align: "center",
        },
        {
          colKey: "op",
          title: "操作",
          align: "center",
          width: 250
        },
      ],
      dataLoading: false,
      dialog: {
        parentId: 0,
        objId: 0,
        open_add_edit: false,
        header: '',
        row: null,
      }
    }
  },
  created() {
    this.getList();
  },
  methods: {
    onSubmit() {
      this.getList();
    },
    /** 查询菜单列表 */
    getList() {
      this.dataLoading = true;
      this.$api.kjxm.kpi.list(this.queryParams).then(response => {
        this.data = this.handleTree(response.data, "id");
        this.dataLoading = false;
      }).catch((e) => {
        this.dataLoading = false;
        this.$message.error(e.toString());
      });
    },
    onExpandAllToggle() {
      this.expandAll = !this.expandAll;
      if (this.expandAll) {
        this.$refs.table.expandAll();
      } else {
        this.$refs.table.foldAll();
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const confirmDia = this.$dialog.confirm({
        header: "系统提示",
        body: `是否确认删除名称为"${row.name}"的数据项？`,
        theme: "warning",
        onConfirm: () => {
          confirmDia.update({
            confirmBtn: {
              loading: true,
            },
          })
          this.$api.kjxm.kpi.del(row.id).then(
            () => {
              this.getList();
              this.$message.success("删除成功");
              // 请求成功后，销毁弹框
              confirmDia.destroy();
            }
          ).catch((e) => {
            confirmDia.update({
              confirmBtn: {
                loading: false,
              },
            })
            this.$message.error(e.toString());
          });
        },
      })
    }
  }
}
</script>

<style scoped>

</style>
